import requests

from apis.bzy import Bzy


class Goods(Bzy):
    def create_goods(self, name, spec):
        """
        创建商品
        :param name: 商品名称
        :param spec: 规格名称
        :return: 响应休信息
        """
        create_url = f"{self.base_url}/v1/goodsbase/"
        json = {"goodsName": name, "goodsCode": "LS93932587", "orderEnterStatus": 0,
                "wareList": [{"wareSsid": "1216", "shippingId": "4302390", "wareShippingId": None}], "goodsImage": "",
                "goodsBaseSkuList": [
                    {"skuName": spec, "skuCode": "skuPB2SMFKF", "specification": "", "basicPrice": "100",
                     "inventory": "1000", "shippingId": None}]}
        req = {
            "method": "POST",
            "url": create_url,
            "json": json,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def update_goods(self, name, spec, id):
        """
        修改商品信息
        :param name: 商品名称
        :param spec: 规格名称
        :param id: 商品id
        :return:响应体
        """

        create_url = f"{self.base_url}/v1/goodsbase/{id}"
        json = {"goodsName": name,
                "wareList": [{"wareSsid": "905", "shippingId": "4302390"}],
                "goodsBaseSkuList":
                    [{"skuName": spec, "basicPrice": "1000", "inventory": "1000"}]}
        req = {
            "method": "PUT",
            "url": create_url,
            "json": json,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def search_goods(self, name, spec):
        """
        搜索商品名称
        :param name: 商品名称
        :param spec: 规格名称
        :return: 响应体
        """
        search_url = f"{self.base_url}/v1/goodsbase/"
        req = {
            "method": "GET",
            "url": search_url,
            "params": {
                "saleStatus": 1,
                "goodsName": name,
                "goodsSkuName": spec,
                "page": 1
            },
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def search_remove_goods(self, name):
        """
        搜索商品名称
        :param name: 商品名称
        :param spec: 规格名称
        :return: 响应体
        """
        search_url = f"{self.base_url}/v1/goodsbase/"
        req = {
            "method": "GET",
            "url": search_url,
            "params": {
                "saleStatus": 0,
                "goodsName": name,
                "page": 1
            },
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def remove_goods(self, sku_id, message):
        """
        下架商品
        :param sku_id: 规格id
        :param message: 备注
        :return: 响应体
        """
        remove_url = f"http://api.wxorder.taover.com/v1/wxordergoodsbasesku/updownstatus/0/sku/{sku_id}"
        json = {"managerNickname": [], "manager": [], "sendMessageStatus": "0", "message": message}
        req = {
            "method": "PUT",
            "url": remove_url,
            "json": json,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def listing_goods(self, sku_id, message):
        """
        上架商品
        :param sku_id: 规格id
        :param message: 备注
        :return: 响应体
        """
        listing_url = f"http://api.wxorder.taover.com/v1/wxordergoodsbasesku/updownstatus/1/sku/{sku_id}"
        json = {"sendMessageStatus": "0", "message": message}
        req = {
            "method": "PUT",
            "url": listing_url,
            "json": json,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def delete_goods(self, id):
        """
        删除商品
        :param id: 规格id
        :param message: 备注
        :return: 响应体
        """
        listing_url = f"http://api.wxorder.taover.com/v1/goodsbase/deleteGoods?idList={id}&"
        req = {
            "method": "DELETE",
            "url": listing_url,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r

    def create_goods_fail(self, goodsName, skuName):
        """
        不添加仓库信息，添加商品失败
        :param name: 商品名称
        :param spec: 规格名称
        :return: 响应休信息
        """
        create_url = f"{self.base_url}/v1/goodsbase/"
        json = {
            "goodsName": goodsName,
            "goodsCode": "47724208",
            "orderEnterStatus": 0,
            "wareList": [

            ],
            "goodsImage": "",
            "goodsBaseSkuList": [
                {
                    "skuName": skuName,
                    "skuCode": "skuO6IH1QB1",
                    "specification": "",
                    "basicPrice": "0.00",
                    "inventory": "0",
                    "shippingId": None
                }
            ]
        }
        req = {
            "method": "POST",
            "url": create_url,
            "json": json,
            "headers": {
                "Authorization": self.token
            }
        }
        r = self.send_api(req)
        return r
